﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App_Code;

public partial class ChangeDataCaptureAuditControl : System.Web.UI.UserControl
{

    public int ItemId
    {
        get
        {
            if (ViewState["ItemdId"] != null)
                return ChangeDataCaptureAuditController.GetInt(ViewState["ItemdId"].ToString());
            else
                return -1;
        }
        set { ViewState["ItemdId"] = value.ToString(); }
    }
    public string TableName
    {
        get
        {
            if (ViewState["TableName"] != null)
                return ViewState["TableName"].ToString();
            else
                return "";
        }
        set { ViewState["TableName"] = value.ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;

        if (Request["TableName"]!=null)
            TableName = Request["TableName"];
        if (Request["itemId"]!=null)
            ItemId = ChangeDataCaptureAuditController.GetInt(Request["itemId"]);

        var controller = new ChangeDataCaptureAuditController();
        var transactionsData = controller.GetTransList(TableName, ItemId);

        grdItems.DataSource = transactionsData;
        grdItems.DataBind();
    }

    protected void grdItems_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        int parent_index = e.NewEditIndex;

        //to set the edit index of the Parent grid with that of the current row
        grdItems.EditIndex = parent_index;
        grdItems.DataBind();
        //find the pubid_lbl containing pub_id in that particular row by using findcontrol method
        GridViewRow row = grdItems.Rows[parent_index];
      

        //save pub_id and edit_index in session for childgridview's use
        //Session["PubID"] = Convert.ToInt32(pub_id_lbl.Text);
        //Session["ParentGridViewIndex"] = parent_index;

    }
    protected void grdItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        var transID = grdItems.SelectedDataKey["TransID"].ToString() ;

        var row = grdItems.SelectedRow;
        if(row !=null )
        {
            var childGridView = row.FindControl("ChildGridView"); 
            if(childGridView!=null )
            {
                GridView childGridView1 = childGridView as GridView;
                var controller = new ChangeDataCaptureAuditController();
                var changes = controller.GetChanges(TableName, transID);
                childGridView1.DataSource = changes;
                childGridView1.DataBind();
            }
        }
        
    }
}